package com.sxl.task;

import com.alibaba.excel.util.DateUtils;
import com.sxl.controller.allocationSwitchController;
import com.sxl.domain.Askers;
import com.sxl.domain.Students;
import com.sxl.service.AskersService;
import com.sxl.service.StudentsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @author shkstart
 * @create 2022-05-13 上午 10:21
 */
@Slf4j
@Component
@EnableScheduling//开启定时任务调度
public class AutoAllocation {
    @Autowired
    private AskersService askersService;
    @Autowired
    private StudentsService studentsService;
//可以写n个job
    //每五分钟执行一次
    @Scheduled(cron = "0 0/1 * * * ?")   //秒分时日月周
    public void allocationAskerJob(){
        log.info("===========自动分配咨询师任务开始执行，开始时间+"+ DateUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
     //获取开关值，如果开关是关闭，直接结束
        String autoAllocationSwitch= allocationSwitchController.autoAllocateSwitch;
        if ("off".equals(autoAllocationSwitch)){
            log.info("------分量开关已关闭，不执行自动分配,请手动分配------");
            return;
        }
        //获取当天已签到的咨询师，如果无数据直接结束
        List<Askers> askersList=askersService.findTodayCheckAskers();
        if (askersList.size()==0){
            log.info("------当前没有可用的已签到的咨询师，结束自动分配------");
            return;
        }
        //没有分配咨询师的学生，结束分配
        List<Students> studentsList=askersService.findNotNetStu();
        if (studentsList.size()==0){
            log.info("------没有待分配的学生，结束自动分配------");
            return;
        }
        //4、开始分配根据分量权重依次循环分配

        int stuNum=studentsList.size();
        int askerNum=askersList.size();
        if (stuNum<=askerNum){
            for (int i=0 ; i<stuNum;i++){
                studentsList.get(i).setAskerId(askersList.get(i).getAskerId());
                studentsList.get(i).setZiXunName(askersList.get(i).getAskerName());
            }
        }
          if (stuNum>askerNum){
              //计算整轮
              int zhenglun=stuNum/askerNum;
              //计算剩余的学生人数
              int temp=0;
              int shengyuStuNum=stuNum%askerNum;
              for (int a=0;a<zhenglun;a++){
                  for (int i=0;i<askerNum;i++){
                      studentsList.get(temp).setAskerId(askersList.get(i).getAskerId());
                      studentsList.get(temp).setZiXunName(askersList.get(i).getAskerName());
                      temp++;
                  }
              }
              //循环剩余的学生人数
              for (int b=0;b<shengyuStuNum;b++){
                  studentsList.get(temp).setAskerId(askersList.get(b).getAskerId());
                  studentsList.get(temp).setZiXunName(askersList.get(b).getAskerName());
                  temp++;
              }
          }
          studentsList.forEach(s->{
              s.setStuStatus(2);
          });
        //5提交到数据库
        boolean rsl=studentsService.updateBatchById(studentsList);
        log.info("------已完成自动分配，结果是"+rsl);

        log.info("------自动分配咨询师结束执行，结束时间"+DateUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
    }
}
